/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "ticket")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Ticket.findAll", query = "SELECT t FROM Ticket t"),
    @NamedQuery(name = "Ticket.findByIdTicket", query = "SELECT t FROM Ticket t WHERE t.idTicket = :idTicket"),
    @NamedQuery(name = "Ticket.findByIdentificador", query = "SELECT t FROM Ticket t WHERE t.identificador = :identificador"),
    @NamedQuery(name = "Ticket.findByFecha", query = "SELECT t FROM Ticket t WHERE t.fecha = :fecha")})
public class Ticket implements Serializable {
    @OneToMany(mappedBy = "reserva")
    private List<Ticket> ticketList;
    @JoinColumn(name = "RESERVA", referencedColumnName = "ID_TICKET")
    @ManyToOne
    private Ticket reserva;
    @JoinColumn(name = "TIPO_TICKET", referencedColumnName = "ID_TIPO_TICKET")
    @ManyToOne(optional = false)
    private TipoTicket tipoTicket;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ticket")
    private List<LineaTicket> lineaTicketList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ticket")
    private List<TicketFormaPago> ticketFormaPagoList;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_TICKET")
    private Integer idTicket;
    @Basic(optional = false)
    @NotNull
    @Column(name = "IDENTIFICADOR")
    private long identificador;
    @Basic(optional = false)
    @NotNull
    @Column(name = "FECHA")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fecha;
    @JoinColumn(name = "USUARIO", referencedColumnName = "ID_USUARIO")
    @ManyToOne(optional = false)
    private Usuario usuario;
    @JoinColumn(name = "CAJA", referencedColumnName = "ID_CAJA")
    @ManyToOne(optional = false)
    private Caja caja;
    @JoinColumn(name = "CLIENTE", referencedColumnName = "ID_CLIENTE")
    @ManyToOne(optional = true)
    private Cliente cliente;

    public Ticket() {
    }

    public Ticket(Integer idTicket) {
        this.idTicket = idTicket;
    }

    public Ticket(Integer idTicket, long identificador, Date fecha) {
        this.idTicket = idTicket;
        this.identificador = identificador;
        this.fecha = fecha;
    }

    public Integer getIdTicket() {
        return idTicket;
    }

    public void setIdTicket(Integer idTicket) {
        this.idTicket = idTicket;
    }

    public long getIdentificador() {
        return identificador;
    }

    public void setIdentificador(long identificador) {
        this.identificador = identificador;
    }

    public Date getFecha() {
        return fecha;
    }

    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    public Caja getCaja() {
        return caja;
    }

    public void setCaja(Caja caja) {
        this.caja = caja;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idTicket != null ? idTicket.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Ticket)) {
            return false;
        }
        Ticket other = (Ticket) object;
        if ((this.idTicket == null && other.idTicket != null) || (this.idTicket != null && !this.idTicket.equals(other.idTicket))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Ticket[ idTicket=" + idTicket + " ]";
    }

    @XmlTransient
    public List<TicketFormaPago> getTicketFormaPagoList() {
        return ticketFormaPagoList;
    }

    public void setTicketFormaPagoList(List<TicketFormaPago> ticketFormaPagoList) {
        this.ticketFormaPagoList = ticketFormaPagoList;
    }

    @XmlTransient
    public List<LineaTicket> getLineaTicketList() {
        return lineaTicketList;
    }

    public void setLineaTicketList(List<LineaTicket> lineaTicketList) {
        this.lineaTicketList = lineaTicketList;
    }
    
    public Integer getUnidades(){
        Integer unidades=0;
        for (LineaTicket lineaTicket : lineaTicketList) {
            unidades+=lineaTicket.getCantidad();
        }
        return unidades;
    }
    
    public BigDecimal getTotal(){
        BigDecimal total=BigDecimal.ZERO;
        if(lineaTicketList!=null){
            for (LineaTicket lineaTicket : lineaTicketList) {
                total=total.add(new BigDecimal(lineaTicket.getCantidad()).multiply(lineaTicket.getTotal()));
            }  
        }
        return total;
    }
    
    public BigDecimal getTotalPagado(){
        BigDecimal total=BigDecimal.ZERO;
        if(ticketFormaPagoList!=null){
            for (TicketFormaPago formaPago : ticketFormaPagoList) {
                total=total.add(formaPago.getCantidad());
            }
        }
        return total;
    }

    @XmlTransient
    public List<Ticket> getTicketList() {
        return ticketList;
    }

    public void setTicketList(List<Ticket> ticketList) {
        this.ticketList = ticketList;
    }

    public Ticket getReserva() {
        return reserva;
    }

    public void setReserva(Ticket reserva) {
        this.reserva = reserva;
    }

    public TipoTicket getTipoTicket() {
        return tipoTicket;
    }

    public void setTipoTicket(TipoTicket tipoTicket) {
        this.tipoTicket = tipoTicket;
    }
    
    public BigDecimal getPagadoEfectivo(){
        BigDecimal efectivo=BigDecimal.ZERO;
        if(ticketFormaPagoList!=null){
            for (TicketFormaPago ticketFormaPago : ticketFormaPagoList) {
                if(ticketFormaPago.getFormaPago().getIdFormaPago()==4){
                    efectivo=efectivo.add(ticketFormaPago.getCantidad());
                }
            }  
        }
        return efectivo;
    }
    
    public BigDecimal getPagadoVisa(){
        BigDecimal visa=BigDecimal.ZERO;
        if(ticketFormaPagoList!=null){
            for (TicketFormaPago ticketFormaPago : ticketFormaPagoList) {
                if(ticketFormaPago.getFormaPago().getIdFormaPago()==5){
                    visa=visa.add(ticketFormaPago.getCantidad());
                }
            }  
        }
        return visa;
    }
    
    public BigDecimal getPagadoAmex(){
        BigDecimal amex=BigDecimal.ZERO;
        if(ticketFormaPagoList!=null){
            for (TicketFormaPago ticketFormaPago : ticketFormaPagoList) {
                if(ticketFormaPago.getFormaPago().getIdFormaPago()==8){
                    amex=amex.add(ticketFormaPago.getCantidad());
                }
            }  
        }
        return amex;
    }
    
    public BigDecimal getPagadoMasterCard(){
        BigDecimal masterCard=BigDecimal.ZERO;
        if(ticketFormaPagoList!=null){
            for (TicketFormaPago ticketFormaPago : ticketFormaPagoList) {
                if(ticketFormaPago.getFormaPago().getIdFormaPago()==9){
                    masterCard=masterCard.add(ticketFormaPago.getCantidad());
                }
            }  
        }
        return masterCard;
    }
    
    public BigDecimal getPagadoMaestro(){
        BigDecimal maestro=BigDecimal.ZERO;
        if(ticketFormaPagoList!=null){
            for (TicketFormaPago ticketFormaPago : ticketFormaPagoList) {
                if(ticketFormaPago.getFormaPago().getIdFormaPago()==10){
                    maestro=maestro.add(ticketFormaPago.getCantidad());
                }
            }  
        }
        return maestro;
    }
}
